package com.tfswx.ocrengine.executor;

import com.tfswx.ocrengine.dto.OcrOutputDTO;
import com.tfswx.ocrengine.enums.YqlxEnum;
import com.tfswx.ocrengine.exception.AppException;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Ricky
 * @date 2021/1/11
 */
@Data
public abstract class AbstractOcrExecutor {
    private String host;

    private static final Map<Long, Integer> MAP = new HashMap<>();

    private static final int MAX_ERR_COUNT = 9;

    public OcrOutputDTO execute(String wjnr) throws EngineException {
        try {
            return run(wjnr);
        } catch (EngineException e) {
            throw e;
        } catch (Exception e) {
            throw new AppException("Ocr识别失败：" + e.getMessage(), e);
        }
    }

    /**
     * 实际运行逻辑实现
     *
     * @param wjnr 图片Base64
     * @return OcrOutputDTO
     * @throws EngineException EngineException
     */
    public abstract OcrOutputDTO run(String wjnr) throws EngineException;

    public static AbstractOcrExecutor getInstance(YqlxEnum yqlxEnum) {
        AbstractOcrExecutor ocrExecutor = null;
        if (yqlxEnum == YqlxEnum.XUNFEI) {
            ocrExecutor = new XunfeiOcrExecutor();
        } else if (yqlxEnum == YqlxEnum.ALI) {
            ocrExecutor = new AliOcrExecutor();
        } else if (yqlxEnum == YqlxEnum.HEHE) {
            ocrExecutor = new HeheOcrExecutor();
        }
        if (ocrExecutor != null) {
            return ocrExecutor;
        }
        throw new AppException("没有该引擎类型");
    }

    public static class EngineException extends Exception {

        public EngineException(String message) {
            super(message);
        }

        public EngineException(String message, Throwable e) {
            super(message, e);
        }
    }
}
